<template>
  <div style="text-align: left">患者信息:
    <span style="font-size: 16px;background-color: beige;color: #42b983">
      姓名:{{ info.realName }}
      病历号:{{ info.caseNumber }}
      年龄:{{ info.age }}
      性别:{{ info.sex }}

    </span>
  </div>

  <hr/>
  <div style="text-align: left">检查费用:{{ totalPrice }}元</div>
  <el-form label-width="auto" style="max-width: 100%" :model="formItem">


    <el-form-item label="诊断">
      <el-checkbox-group style="width: 100%" v-model="formItem.medicalTechList">
        <el-table :data="medicalList" style="width: 100%">


          <el-table-column label="选择">
            <template #default="{ row }">
              <el-checkbox
                  :value="row.id+','+row.techPrice"
                  name="medicalTechList">
              </el-checkbox>
            </template>
          </el-table-column>


          <el-table-column prop="techCode" label="检查编码"/>
          <el-table-column prop="techName" label="检查名称"/>
          <el-table-column prop="techFormat" label="检查规格"/>
          <el-table-column prop="techPrice" label="单价"/>
          <el-table-column prop="priceType" label="费用类型"/>

        </el-table>
        <el-button type="primary">添加</el-button>
        <el-button type="success">删除</el-button>

      </el-checkbox-group>

    </el-form-item>



    <el-form-item label="目的要求">
      <el-input v-model="formItem.checkInfo" style="height: 60px" />
    </el-form-item>
    <el-form-item label="检查部位">
      <el-input v-model="formItem.checkPosition" style="height: 60px"/>
    </el-form-item>
    <el-form-item label="备注">
      <el-input v-model="formItem.checkRemark" style="height: 60px"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="saveCheckReq">申请提交</el-button>
      <el-button type="success">清空表格</el-button>
    </el-form-item>

  </el-form>

</template>

<script setup>

import {computed, onMounted, reactive, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";


//定义病人信息数据
const info = reactive({
  realName: '',
  caseNumber: '',
  age: '',
  sex: ''
});

//定义检查项列表集合
const medicalList = ref([]);
let id = 0; //挂号Id
if (sessionStorage.getItem("sessionData")) {
  // 获取sessionStorage
  const sessionData = JSON.parse(sessionStorage.getItem('sessionData'));
  id = sessionData.id;
  //sessionStorage.removeItem("sessionData");
}

//定义表单数据
const formItem=reactive({
  id:'',
  registerId:id,
  medicalTechnologyId:'',
  checkInfo:'',
  checkPosition:'',
  creationTime:'',
  checkEmployeeId:'',
  inputcheckEmployeeId:'',
  checkTime:'',
  checkResult:'',
  checkState:'',
  checkRemark:'',
  medicalTechnologyIdList:[],
  medicalTechList:[]


});

onMounted(() => {
  getRegisterInfo();
  loadMedicalItem();

});

/*定义函数加载病人信息*/
function getRegisterInfo() {

  axios.get("http://localhost/his/queryRegiserInfo/" + id)
      .then((response) => {
        info.realName = response.data.realName;
        info.caseNumber = response.data.caseNumber;
        info.age = response.data.age;
        info.sex = response.data.gender;
        //删除sessionStorage对象中保存的键值对

      })
      .catch((error) => {
        console.log(error);
      });
}



/*定义函数发生请求，加载检查项列表*/
function loadMedicalItem() {
  axios.get("http://localhost/his/queryMedicalItem")
      .then((response) => {
        medicalList.value = response.data;
      })
      .catch((error) => {
        console.log(error);
      });
}

/*定义函数发生保存检查单的ajax请求*/
function saveCheckReq(){

  formItem.medicalTechList.forEach((item)=>{
    //console.log(item);
    var temp=item+"";
    temp=temp.substring(0,temp.indexOf(","));
    formItem.medicalTechnologyIdList.push(parseInt(temp));
    //console.log(formItem.medicalTechnologyIdList);
  });
  axios.post("http://localhost/his/saveCheckRequest",formItem)
      .then((response) => {
        ElMessage(response.data.msg);
      })
      .catch((error) => {
        console.log(error);
      });
}


//定义检查费用总结额
const totalPrice = computed(()=>{
  console.log("===computed");
  var sum=0;
  formItem.medicalTechList.forEach((item)=>{
    //console.log(item);
    var temp=item+"";
    temp=temp.substring((temp.indexOf(",")+1));
    //console.log(temp);
    sum=sum+parseFloat(temp);
  });
  return sum;
});
</script>

<style scoped>

</style>